package com.hliushi.redis.controller;

import com.hliushi.redis.config.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Set;

/**
 * @author llhuang10
 * @date 2021/8/14 21:52
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class RankController {

    @Resource
    private RedisTemplate redisTemplate;


    @GetMapping("/get-hour")
    public Set getHour() {
        long hour = System.currentTimeMillis() / (1000 * 60 * 60);
        // zrevrange 返回有序集key中, 指定区间内的成员, 降序
        return (Set<ZSetOperations.TypedTuple<Integer>>) redisTemplate.opsForZSet().reverseRangeWithScores(Constants.HOUR_KEY + hour, 0, 15);
    }

    @GetMapping("/get-day")
    public Set getDay() {
        return (Set<ZSetOperations.TypedTuple<Integer>>) redisTemplate.opsForZSet().reverseRangeWithScores(Constants.DAY_KEY, 0, 15);
    }

    @GetMapping("/get-week")
    public Set getWeek() {
        return (Set<ZSetOperations.TypedTuple<Integer>>) redisTemplate.opsForZSet().reverseRangeWithScores(Constants.WEEK_KEY, 0, 15);
    }

    @GetMapping("/get-month")
    public Set getMonth() {
        return (Set<ZSetOperations.TypedTuple<Integer>>) redisTemplate.opsForZSet().reverseRangeWithScores(Constants.MONTH_KEY, 0, 15);
    }


}
